﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LotteryVote.Utility;
using System.IO;
using System.Text.RegularExpressions;
using System.Threading;
using LotteryVote.Entity;

namespace ConsoleApplication1
{
    class Test
    {
        public void Print(string name, int i)
        {
            Console.WriteLine(name + ": " + i + "  " + DateTime.Now.Ticks + "  ");
        }
    }
    class Program
    {
        static Test obj = new Test();
        static string field;
        static void Main(string[] args)
        {
            for (int i = 0; i < 10; i++)
            {
                for (int j = 1; j < 6; j++)
                    ThreadPool.QueueUserWorkItem(it => { obj.Print("U" + j, i); }, null);
                Thread.Sleep(1000);
            }
        }

        static void Print(int name, int i)
        {
            //field = name;
            Console.WriteLine(name + ": " + i + "  " + DateTime.Now.Ticks + "  ");
        }

        static void BuildOrder()
        {
            ThreadPool.SetMaxThreads(10, 10);
            for (int i = 0; i < 10; i++)
            {
                ThreadPool.QueueUserWorkItem(it =>
                {

                    for (int j = 0; j < 20; j++)
                    {
                        Console.WriteLine(string.Format("ThreadId:{0},OrderId:{1}", Thread.CurrentThread.ManagedThreadId, OrderBuilder()));
                    }
                }, null);
            }
        }
        static object _lockHelper = new object();
        static string OrderBuilder()
        {
            lock (_lockHelper)
            {
                return string.Format("{0}{1}", Guid.NewGuid(), DateTime.Now.Ticks);
            }
        }

        static void GetArrangement()
        {

            Console.WriteLine("Input the length:");
            //FullArrangement.length = int.Parse(Console.ReadLine());
            MyArrangement.length = int.Parse(Console.ReadLine()); ;
            //if (FullArrangement.length > FullArrangement.MAX_NUM)
            if (MyArrangement.length > MyArrangement.MAX_NUM)
            {
                Console.WriteLine("Error");
                return;
            }
            Console.WriteLine("Input the numbers:");
            //for (int i = 0; i < FullArrangement.length; i++)
            //    FullArrangement.numbers[i] = int.Parse(Console.ReadLine());
            for (int i = 0; i < MyArrangement.length; i++)
                MyArrangement.Nums[i] = int.Parse(Console.ReadLine());
            Console.WriteLine("The result:");
            //FullArrangement.Arrangement(FullArrangement.numbers, 0);
            MyArrangement.CountArrangement(MyArrangement.Nums, 0);
        }

        static void Parse(string path)
        {
            //DirectoryInfo dir = new DirectoryInfo(path);
            //var files = dir.GetFiles().OrderBy(it => it.Name);
            var filenames = Directory.GetFiles(path);
            var files = filenames.Select(it => new { Name = it, Num = int.Parse(new Regex(@"(?<=chapter)\d+(?=\.html)").Match(it).Value) });
            Regex contentRule = new Regex(@"(?<=\<div\>)(?s).+(?=\<\/div\>)");
            Regex foreRule = new Regex(@"\<\w+\>");
            Regex backRule = new Regex(@"\<\/\w+\>");
            MemoryStream ms = new MemoryStream();
            foreach (var file in files.OrderBy(it => it.Num))
            {
                StreamReader sr = new StreamReader(file.Name);
                string content = sr.ReadToEnd();
                Match match = contentRule.Match(content);
                if (!match.Success) continue;
                content = match.Value;
                content = foreRule.Replace(content, string.Empty);
                content = backRule.Replace(content, "\r\n");
                content = new Regex(@"(\r\n)+").Replace(content, "\r\n");
                File.AppendAllText(path + "\\book.txt", content, Encoding.GetEncoding("GB2312"));
            }
        }
    }
}
